<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="交易对" prop="instId">
        <el-input
          v-model="queryParams.instId"
          placeholder="请输入交易对"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据维度" prop="bar">
        <el-input
          v-model="queryParams.bar"
          placeholder="请输入数据维度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单向振幅" prop="dxzf">
        <el-input
          v-model="queryParams.dxzf"
          placeholder="请输入单向振幅"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- 添加观测时间查询条件 -->
      <el-form-item label="观测时间">
        <el-date-picker
          v-model="daterangeGcTime"
          style="width: 240px"
          value-format="yyyy-MM-dd 07:59:59"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['bussiness:history:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['bussiness:history:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['bussiness:history:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['bussiness:history:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="historyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="交易对" align="center" >
        <template slot-scope="scope">
          <div style="text-align: center; line-height: 1.5;">
            <div style="text-align: left; display: inline-block; line-height: 1.5;">
              <div >{{ formatInstId(scope.row.instId) }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="时间维度" align="center" prop="bar" />
      <el-table-column label="开盘价" align="center" prop="kpj" />
      <el-table-column label="实时价" align="center" prop="ssj" />
      <el-table-column label="最高价" align="center" prop="zgj" />
      <el-table-column label="最低价" align="center" prop="zdj" />
      <el-table-column label="实时振幅" align="center" prop="sszf" />
      <el-table-column label="最大振幅" align="center" prop="zdzf" />
      <el-table-column label="单向振幅" align="center" prop="dxzf" />
      <el-table-column label="涨跌方向" align="center" prop="fx" />
      <el-table-column label="观测时间" align="center" prop="gcTime" width="180">
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listHistory, getHistory, delHistory, addHistory, updateHistory } from "@/api/bussiness/history"

export default {
  name: "History",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 历史数据表格数据
      historyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 观测时间时间范围
      daterangeGcTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        instId: null,
        dxzf: null,
        bar: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.setDefaultDateRange()
    this.getList()
  },
  methods: {
    /** 设置默认日期范围 - 当月第一天到今日 */
    setDefaultDateRange() {
      const now = new Date()
      const firstDay = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1)

      // 格式化日期为 yyyy-MM-dd
      const formatDate = (date) => {
        const year = date.getFullYear()
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        const day = date.getDate().toString().padStart(2, '0')
        return `${year}-${month}-${day}`
      }

      // 设置默认日期范围
      this.daterangeGcTime = [
        formatDate(firstDay) + ' 07:59:59',
        formatDate(now) + ' 07:59:59'
      ]
    },

    /** 查询历史数据列表 */
    getList() {
      this.loading = true
      // 添加观测时间参数处理
      this.queryParams.params = {}
      if (null != this.daterangeGcTime && '' != this.daterangeGcTime) {
        this.queryParams.params["beginGcTime"] = this.daterangeGcTime[0]
        this.queryParams.params["endGcTime"] = this.daterangeGcTime[1]
      }
      listHistory(this.queryParams).then(response => {
        this.historyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        instId: null,
        kpj: null,
        ssj: null,
        zgj: null,
        zdj: null,
        sszf: null,
        zdzf: null,
        fx: null,
        dxzf: null,
        bar: null,
        gcTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 重置观测时间范围
      this.setDefaultDateRange()
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加历史数据"
    },
    /** 格式化币种显示，去掉-USDT-SWAP */
    formatInstId(instId) {
      if (!instId) return ''
      return instId.replace('-USDT', '')
    },

    /** 导出按钮操作 */
    handleExport() {
      // 导出时也需要包含观测时间参数
      const exportParams = {
        ...this.queryParams
      }
      // 添加时间范围参数到导出参数
      if (null != this.daterangeGcTime && '' != this.daterangeGcTime) {
        exportParams.beginGcTime = this.daterangeGcTime[0]
        exportParams.endGcTime = this.daterangeGcTime[1]
      }

      this.download('bussiness/history/export', exportParams, `history_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
